package com.xiaoye.qbproject.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.CountryRegion;
import com.xiaoye.qbproject.service.CountryRegionService;
import com.xiaoye.qbproject.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description : 国家或地区
 * @create : 2024-12-27 1:41
 */
@RestController
@RequestMapping("/countryRegion")
public class CountryRegionController extends BaseController {
    @Autowired
    CountryRegionService countryRegionService;

    @GetMapping("")
    public AjaxResult list(CountryRegion countryRegion) {
        String name = countryRegion.getName();
        LambdaQueryWrapper<CountryRegion> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(CountryRegion::getName, name);
        }
        List<CountryRegion> countryRegions = countryRegionService.list(queryWrapper);
        return AjaxResult.success(countryRegions);
    }

    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable String id) {
        CountryRegion countryRegion = countryRegionService.getById(id);
        return AjaxResult.success(countryRegion);
    }

    /**
     * @description: 查询国家或地区树形结构
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-27 1:54
     */

/*    @RequestMapping("/tree")
    public AjaxResult tree() {
        // 查询所有国家或地区信息
        List<CountryRegion> countryRegionList = countryRegionService.list();
        if (countryRegionList.isEmpty()) {
            return AjaxResult.error("暂无数据");
        } else if (countryRegionList.size() == 1) {
            return AjaxResult.success(countryRegionList);
        }
        // 组装成树形结构
        /List<CountryRegion> countryRegions = countryRegionService.buildTree(countryRegionList);
        return AjaxResult.success(countryRegions);
    }*/

    /**
     * @param countryRegion
     * @description: 添加国家或地区
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-27 1:57
     */

    @PostMapping("")
    public AjaxResult add(@RequestBody CountryRegion countryRegion) {
        countryRegionService.delCache();
        return toAjax(countryRegionService.save(countryRegion));
    }

    /**
     * @param countryRegion
     * @description: 更新国家或地区
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-27 1:58
     */

    @PutMapping
    public AjaxResult update(@RequestBody CountryRegion countryRegion) {
        countryRegionService.delCache();
        return toAjax(countryRegionService.updateById(countryRegion));
    }

    /**
     * @param id
     * @description: 删除国家或地区
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-27 1:58
     */

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        countryRegionService.delCache();
        return toAjax(countryRegionService.removeById(id));
    }

    /**
     * @param countryId
     * @description: 获取国家或地区 下的兵力编程编组
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-30 12:01
     */

    @GetMapping("/getBlBcTreeByCountryId/{countryId}")
    public AjaxResult getBlBcTreeByCountryId(@PathVariable String countryId) {

        CountryRegion country = countryRegionService.getBlBcTreeByCountryId(countryId);
        return AjaxResult.success(country);
    }
}
